package com.lz.sports.dialog

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lz.library.adapter.SimpleAdapter
import com.lz.library.adapter.SimpleViewHolder
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.click
import com.lz.sports.R
import com.lz.sports.bean.ParcelableString
import com.lz.sports.databinding.DialogSelectBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class ParcelableSelectDialog : BaseDialogFragment<DialogSelectBinding>() {

    companion object {

        fun show(
            activity: FragmentActivity,
            title: String,
            current: String?,
            contents: ArrayList<String>,
            requestCode: Int = -1
        ): ParcelableSelectDialog {
            val dialog = ParcelableSelectDialog().also {
                it.arguments = Bundle().apply {
                    putString("title", title)
                    putString("current", current)
                    putStringArrayList("contents", contents)
                    putInt("requestCode", requestCode)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun show(
            fragment: Fragment,
            title: String,
            current: String?,
            contents: ArrayList<String>,
            requestCode: Int = -1
        ): ParcelableSelectDialog {
            val dialog = ParcelableSelectDialog().also {
                it.arguments = Bundle().apply {
                    putString("title", title)
                    putString("current", current)
                    putStringArrayList("contents", contents)
                    putInt("requestCode", requestCode)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }

        fun show(
            activity: FragmentActivity,
            title: String,
            current: ParcelableString?,
            contents: ArrayList<out ParcelableString>,
            requestCode: Int = -1
        ): ParcelableSelectDialog {
            val dialog = ParcelableSelectDialog().also {
                it.arguments = Bundle().apply {
                    putBoolean("isParcelable", true)
                    putString("title", title)
                    putParcelable("current", current)
                    putInt("requestCode", requestCode)
                    putParcelableArrayList("contents", contents)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun show(
            fragment: Fragment,
            title: String,
            current: ParcelableString?,
            contents: ArrayList<out ParcelableString>,
            requestCode: Int = -1
        ): ParcelableSelectDialog {
            val dialog = ParcelableSelectDialog().also {
                it.arguments = Bundle().apply {
                    putBoolean("isParcelable", true)
                    putString("title", title)
                    putParcelable("current", current)
                    putInt("requestCode", requestCode)
                    putParcelableArrayList("contents", contents)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }
    }

    override fun getLayoutRes(): Int = R.layout.dialog_select

    @Suppress("DEPRECATION")
    override fun initData(v: View, savedState: Bundle?) {
        binding.tvTitle.text = arguments!!.getString("title", "")
        lifecycleScope.launch {
            binding.tvCancel.click().collect {
                dismiss()
            }
        }
        binding.rvList.addItemDecoration(com.lz.comm.widget.RecyclerViewDivider.publicDivider())

        val adapter = if (arguments!!.getBoolean("isParcelable", false)) {
            val contents = arguments!!.getParcelableArrayList<ParcelableString>("contents")!!
            ParcelableSelectAdapter(contents, arguments!!.getParcelable("current"))
        } else {
            val contents = arguments!!.getStringArrayList("contents")!!
            SelectAdapter(contents, arguments!!.getString("current"))
        }

        adapter.setOnItemClickListener(object : com.lz.library.adapter.BaseAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                val requestCode = arguments!!.getInt("requestCode")
                if (isFromActivity()) {
                    if (context is OnSelectedListener) {
                        (context as OnSelectedListener).onSelected(this@ParcelableSelectDialog, position, requestCode)
                    }
                } else {
                    if (parentFragment is OnSelectedListener) {
                        (parentFragment as OnSelectedListener).onSelected(this@ParcelableSelectDialog, position, requestCode)
                    }
                }
            }
        })
        binding.rvList.adapter = adapter
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = BottomDialog(requireContext())

    private class ParcelableSelectAdapter(initData: List<ParcelableString>, private val current: ParcelableString?) :
        SimpleAdapter<ParcelableString>(initData) {

        override fun getLayoutResId(viewType: Int): Int = R.layout.item_select_public

        override fun onBindViewHolder(vh: SimpleViewHolder, position: Int) {
            val tv = vh.getView<TextView>(R.id.tv_content)
            val item = getItem(position)
            tv.text = getItem(position).str()
            tv.isSelected = item == current
        }
    }

    private class SelectAdapter(initData: List<String>, private val current: String?) : SimpleAdapter<String>(initData) {

        override fun getLayoutResId(viewType: Int): Int = R.layout.item_select_public

        override fun onBindViewHolder(vh: SimpleViewHolder, position: Int) {
            val tv = vh.getView<TextView>(R.id.tv_content)
            val item = getItem(position)
            tv.text = item
            tv.isSelected = item == current
        }
    }

    interface OnSelectedListener {

        fun onSelected(dialog: ParcelableSelectDialog, position: Int, requestCode: Int)
    }
}